<?php
/**
 * 微信端
 * @author maniac
 */
namespace app\wap\controller;

use think\Controller;
use app\common\model\Orders;
use app\common\model\WechatUsers;
use app\facade\Wechat AS WechatFacade;
use \jssdk\JSSDK;


class Wechat extends Controller
{
    public function wxpay($id = 0)
    {
        WechatFacade::getAccessToken();
        $jssdk       = new JSSDK(config('site.app_id'), config('site.app_secret'));
        $signPackage = $jssdk->GetSignPackage();
        return $this->fetch('', [
            'id'          => $id,
            'signPackage' => $signPackage,
        ]);
    }
    
    public function jsapi()
    {
        $id = input('post.id/d', 0);
        if (!$id) {
            return j(1, '订单不存在');
        }
        $order = Orders::field('order_no,out_trade_no,userid,pay_amount')->where('id', $id)->find();
        if (!$order) {
            return j(1, '订单不存在');
        }
        $user = WechatUsers::field('openid')->where('id', $order['userid'])->find();
        $order['openid'] = $user['openid'];
        $jsapi = WechatFacade::unifiedorder($order);
        if ($jsapi) {
            return j(0, '统一下单成功', $jsapi);
        }
        return j(1, '统一下单失败');
    }
}
